package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import com.yeyaomai.dksns.flowis.dao.KeywordDao;
import com.yeyaomai.dksns.flowis.domain.EngineKeyword;
import com.yeyaomai.dksns.flowis.domain.Keyword;
import com.yeyaomai.dksns.flowis.domain.ReferDomain;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.KeywordService;

@Service
public class KeywordServiceImpl implements KeywordService {

	@Resource
	private KeywordDao keywordDao;

	@Override
	public void insertKeyword(Keyword keyword) {

		keywordDao.insertKeyword(keyword);
	}

	@Override
	public List<Keyword> queryKeywordByDate(String st ,String et) {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("st", st);
		map.put("et", et);
		return keywordDao.queryKeywordByDate(map);
	}

	@Override
	public List<EngineKeyword> queryEngineKeywordByDate(String keyword, String st ,String et) {
		
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("keyword", keyword);
		map.put("st", st);
		map.put("et", et);
		return keywordDao.queryEngineKeywordByDate(map);
	}
	
	@Override
	public Page<Keyword> getSplit(FlowisQueryOrder order) {
		
		PageRequestOrder page = new PageRequestOrder(Integer.valueOf(order.getNumber()) , SplitConstant.PAGE_SIZE);
		List<Keyword> keywords = queryKeywordByDate(order.getStartTime(), order.getEndTime());
		List<Keyword> kws =	interception(keywords, page.start() ,page.end());
		Page<Keyword> result = new PageImplResult<Keyword>(kws, page, keywords.size());
		return result;
	}
	
	public List<Keyword> interception(List<Keyword> keywords ,Integer start , Integer ent){
		
		List<Keyword> list = new ArrayList<Keyword>();
		ent =  ent>keywords.size() ? keywords.size() : ent ;
		for(int i = start; i< ent; i++ ){
			list.add(keywords.get(i));
		}
		return list;
	}
	
}
